<template>
  <el-dialog
    v-model="dialogVisible"
    :destroy-on-close="true"
    :align-center="true"
    width="50%"
    title="开票管理"
    style="padding: 20px"
  >
    <el-form
      ref="billFormRef"
      :rules="rules"
      :disabled="dialogData.isView"
      :model="dialogData.itemInfo"
      label-width="120px"
      label-suffix=" :"
      :hide-required-asterisk="dialogData.isView"
    >
      <el-tabs v-model="dialogData.itemInfo.type" class="demo-tabs" stretch >
        <!-- <el-tab-pane label="" name="0"></el-tab-pane> -->
        <el-tab-pane label="企业单位" name="1">
          <el-form-item label="公司名称">
            <el-input v-model="dialogData.itemInfo.companyName" placeholder="公司名称"></el-input>
          </el-form-item>
          <el-form-item label="公司税号">
            <el-input v-model="dialogData.itemInfo.billNo" placeholder="公司税号"></el-input>
          </el-form-item>
          <el-form-item label="注册地址">
            <el-input v-model="dialogData.itemInfo.billAddress" placeholder="注册地址"></el-input>
          </el-form-item>
          <el-form-item label="注册电话">
            <el-input v-model="dialogData.itemInfo.phone" placeholder="注册电话"></el-input>
          </el-form-item>
          <el-form-item label="开户银行">
            <el-input v-model="dialogData.itemInfo.bankName" placeholder="开户银行"></el-input>
          </el-form-item>
          <el-form-item label="银行帐户">
            <el-input v-model="dialogData.itemInfo.bankAccount" placeholder="银行帐户"></el-input>
          </el-form-item>
          <el-form-item label="收票邮箱">
            <el-input v-model="dialogData.itemInfo.email" placeholder="收票邮箱"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="个人/非企业" name="2" disabled>
          <el-form-item label="姓名">
            <el-input v-model="dialogData.itemInfo.companyName" placeholder="姓名"></el-input>
          </el-form-item>
          <el-form-item label="身份证号">
            <el-input v-model="dialogData.itemInfo.billNo" placeholder="身份证号"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="dialogData.itemInfo.billAddress" placeholder="邮箱"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="dialogData.itemInfo.phone" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item label="开户行">
            <el-input v-model="dialogData.itemInfo.bankName" placeholder="开户行"></el-input>
          </el-form-item>
          <el-form-item label="银行卡号">
            <el-input v-model="dialogData.itemInfo.bankAccount" placeholder="银行卡号"></el-input>
          </el-form-item>
          <el-form-item label="收票邮箱">
            <el-input v-model="dialogData.itemInfo.email" placeholder="收票邮箱"></el-input>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" v-show="!dialogData.isView" @click="handleSubmit" :loading="btnLoading">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { getOptTitle, OptEnum } from "@/biz/eenum/appEnum";
import { DialogParam } from "@/biz/interface/DialogType";
import { getBillRiseInfoApi, saveBillRiseApi } from "@/api/modules/cpyjob/billrise";
import { BillRiseEntity } from "@/biz/interface/BillType";

//页面的数据
interface flexjobAbnormalDialogOption extends DialogParam {
  //弹出表单的标题
  title: string;
  //是否是查看状态，查看状态是不可以提交数据
  isEdit: boolean;
  //当前的数据对象
  itemInfo: BillRiseEntity;
  //其它的数据都继续在这里加
}
//提交按钮，避免多次提交
const btnLoading = ref(false);
//弹出框的参数
const dialogVisible = ref(false);
//当前表单数据在这里初始化
const dialogData = reactive<flexjobAbnormalDialogOption>({
  optType: OptEnum.UNKNOW,
  title: "",
  isEdit: false,
  itemInfo: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DialogParam): void => {
  dialogData.optType = params.optType;
  dialogData.rowData = params.rowData;
  dialogData.isEdit = dialogData.optType === OptEnum.EDIT;
  dialogData.title = getOptTitle(dialogData.optType);

  if (dialogData.rowData) {
    initItemInfo();
    getItemInfo(dialogData.rowData.id);
  }

  dialogVisible.value = true;
};

//初始化页面的数据
const initItemInfo = (): void => {
  dialogData.itemInfo = {
    type: "1"
  };
};

// // 修改的时候不可以修改类型
// const checkEdit = () => {
//   let p = new Promise((resolve, reject) => {});
//   return p;
// };

//获取当前数据的详情，直接调用接口获取数据
const getItemInfo = async (id: string) => {
  if (id) {
    // console.log(id);
    let { data } = await getBillRiseInfoApi({ id });
    dialogData.itemInfo = data as unknown as BillRiseEntity;
    if (dialogData.itemInfo) {
      dialogData.itemInfo.type = dialogData.itemInfo.type + "";
    }
  }
};

//数据较验
const rules = reactive({
  billNo: [{ required: true, message: "请填写公司税号", trigger: "blur" }],
  companyName: [{ required: true, message: "请填写公司名称", trigger: "blur" }],
  idCard: [{ required: true, message: "请填写身份证号", trigger: "blur" }],
  userName: [{ required: true, message: "请填写姓名", trigger: "blur" }],
  notEmpty: [{ required: true, message: "不能为空", trigger: "blur" }]
});

// 提交数据（新增/编辑）
const billFormRef = ref<FormInstance>();
const handleSubmit = () => {
  btnLoading.value = true;
  billFormRef.value!.validate(async valid => {
    if (!valid) {
      btnLoading.value = false;
      return;
    }
    try {
      //提交数据
      let res = await saveBillRiseApi({
        ...dialogData.itemInfo
      });
      if (res.data) {
        ElMessage.success({ message: `发票抬头保存完成！` });
        //刷新列表数据
        // eslint-disable-next-line vue/custom-event-name-casing
        emits("refresh-list");
        dialogVisible.value = false;
      }
    } catch (error) {
    } finally {
      btnLoading.value = false;
    }
  });
};

//页面事件
const emits = defineEmits(["refresh-list"]);

//页面参数
defineExpose({
  acceptParams
});
</script>
<style scoped lang="scss">
.demo-tabs > .el-tabs__header {
  padding-left: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>
